document.write('<link rel="stylesheet" type="text/css" href="'+CSS_PATH+'icon.css"><link rel="stylesheet" type="text/css" href="'+CSS_PATH+'index.css"><script type="text/javascript" src="'+JS_PATH+'socket.io.js"></script><script type="text/javascript" src="'+JS_PATH+'JQuery.js"></script><script type="text/javascript" src="'+JS_PATH+'json2.js"></script><script type="text/javascript" src="'+JS_PATH+'md5.js"></script><script type="text/javascript" src="'+JS_PATH+'post.js"></script><script type="text/javascript" src="'+JS_PATH+'callback.js"></script><script type="text/javascript" src="'+JS_PATH+'user.js"></script><script type="text/javascript" src="'+JS_PATH+'index.js"></script>');
var html='<div id="dianliao">';
html+='<div class="control">';
html+='<div class="tall-win-open"><div class="icon-arrow-left"></div></div>';
html+='<div class="menu">';
html+='<div class="menu-body">';
html+='<div id="user-friend" class="on" value="dl-friend">好友</div><div id="user-recently" value="dl-recently">最近</div></div></div>';
html+='<div id="dl-tip"></div>';
html+='<div class="win-min"><div class="min"></div></div>';
html+='</div>';
html+='<div class="body">';
html+='<div id="dl-menu" class="dl-menu" dl="m-menu">';
html+='<div class="no-wh">';
html+='<div class="dl-menu-content" dl="m-menu" value="m1">';
html+='<div class="menu-c m1" value="m1" dl="m-menu">添加好友</div>';
html+='<div class="menu-c m2" value="m2" dl="m-menu">删除好友</div>';
html+='<div class="menu-c m3" value="m3" dl="m-menu">修改昵称</div>';
html+='<div class="menu-c m4" value="m4" dl="m-menu">收缩窗口</div>';
html+='<div class="menu-c m5" value="m5" dl="m-menu">个人资料</div>';
html+='<div class="menu-c m6" value="m6" dl="m-menu">修改密码</div>';
html+='<div class="menu-c m7" value="m7" dl="m-menu">修改头像</div>';
html+='<div class="menu-c m8" value="m8" dl="m-menu">退出点聊</div>';
html+='<div class="menu-content" dl="m-menu">';
html+='<div class="dl-m-tip" dl="m-menu">';
html+='<div class="tip-title" dl="m-menu">添加好友</div>';
html+='<div class="tip-content" dl="m-menu"></div></div>';
html+='<input id="dl-m-text" class="dl-m-text" type="text" dl="m-menu">';
html+='<div class="dl-m-submit" dl="m-menu">确定</div>';
html+='<div class="dl-sub-res" dl="m-menu"></div></div></div></div>';
html+='<div class="m-font" dl="m-menu">菜单</div></div>';
html+='<div id="dl-friend" class="dl-win on"></div>';
html+='<div id="dl-recently" class="dl-win"></div></div></div>';
html+='<div id="dl-tall_win">';
html+='<div id="dl-tall-win" class="dl-win">';
html+='<div class="tall-header">';
html+='<div class="tall-friend-msg">';
html+='<div class="friend-avatar"></div>';
html+='<div class="friend-name"></div></div>';
html+='<div class="win-min"><div class="exit icon-arrow-right"></div></div></div>';
html+='<div class="tall-menu"><ul id="tall-menu"></ul>';
html+='<div class="tall-menu-clear">清空列表</div></div><div class="dl-msg"></div>';
html+='<div class="msg-post"><div class="msg-add">';
html+='<a id="chat-image-select" title="请选择图片"><i class="icon-camera"></i>图片</a>';
html+='<a id="chat-file-select" title="请选择附件"><i class="icon-folder-open"></i>附件</a></div>';
html+='<div id="msg-post" contenteditable="true"></div><div class="msg-sub"><div id="msg-btn" onclick="postMsg();">发送</div>';
html+='</div></div></div></div>';
document.write(html);
var dianliao=function(){
	var dl={};
	dl.user=user();
	dl.post=post();
	loadAnimate();
	function init(number){
		dl.user.init({'number':number.toString()});
		dl.post.init(dl.user);//建立链接
		dl.post.login(dl.user.getUser());//登陆
		document.onkeydown = function(e) {
			var ev = (typeof event!= 'undefined') ? window.event : e;
			if(ev.keyCode == 13 && document.activeElement.id == "msg-post") {
				postMsg();
			}
			if(ev.keyCode == 13 && document.activeElement.id == "dl-m-text") {
				$('.dl-m-submit').click();
			}
		}
	}
	function regist(u){
		dl.post.regist(u);
	}
	return {
		'init':init,
		'regist':regist,
		'dl':dl
	};
}
function friendClick(a){
	if($(window.event.target).attr('clickOnly')==1){return false;}
	//获得昵称和号码
	var nick=$(a).find('.nick a').html();
	var number=$(a).attr('number');
	var img=$(a).find('img').clone();
	var num=$(a).find('.tip').html()||0;
	//创建窗口、或者重新展示窗口
	displayTallWin(number,nick,img,num);
	dotnerDianliao.dl.user.focusFriend(number);
}
function tallMenuFriend(a,e){
	if($(e.target).attr('clickOnly')==1){return false;}
	$('#tall-menu .on').removeClass('on');
	$(a).addClass('on');
	$('#dl-friend .f-'+$(a).attr('number')).click();
}
function tallMenuFriendClose(a){
	if($(a).parent().hasClass('on')){
		$(a).parent().remove();
		if($('#tall-menu li').length>0){
			$('#tall-menu li').eq(0).click();
			return false;
		}else{
			$('#dianliao .control .tall-win-open div').css('display','none');
			$('#dl-tall-win .dl-msg .msg').css('display','none');
			$('#dl-tall-win .tall-friend-msg .friend-avatar').html('');
			$('#dl-tall-win .tall-friend-msg .friend-name').html('');
			$('#dl-tall_win .tall-header .win-min .exit').click();
		}
	}else{
		$(a).parent().remove();
	}
}
function postMsg(){
	/*var nick=$('dl-tall-win .friend').html();
	var number=$('dl-tall-win .friend').attr('number');*/
	var msg={};
	msg.number=dotnerDianliao.dl.user.getFocus();
	msg.msg=$('#msg-post').html();
	dotnerDianliao.dl.post.gossip(msg);
}
function addRecentlyFriend(number){
	var f=$('#dl-friend .f-'+number);
	$('#dl-recently').prepend(f.clone());
	recentlyOnMouseOver();
}
function recentlyOnMouseOver(){
	$('#dl-recently .friend').hover(function(){
		$(this).prepend('<div class="no-wh" id="f-c-control"><i onclick="recentlyFriendClose(this);" clickonly="1">×</i></div>');
	},function(){
		$('#f-c-control').remove();
	});
}
var animate={};
function loadAnimate(){
	$(window).on('mousedown',function(e){
		if($(e.target).attr('dl')!='m-menu'){
			$('#dianliao .body .dl-menu .dl-menu-content').css('display','none');
		}
	});
	/*主窗口最小化*/
	animate.bodyHeight=$('#dianliao .body').css('height');
	animate.controlWidth=$('#dianliao .control').css('width');
	animate.dlRight=$('#dianliao').css('right');
	$('#dianliao .control .win-min').on('click',function(){
		if($('#dianliao .body').css('height') != '0px'){
			//最小化
			$('#dianliao .body').stop().animate({'height':'0px'},200);
			$('#dianliao').stop().animate({'bottom':parseInt($('#dianliao').css('bottom'))-parseInt(animate.bodyHeight)},200);
			$('#dianliao .win-min .min').removeClass('min').addClass('max');
			$('#dianliao .control .user-name').css('display','block');
			$('#dianliao .control .menu').css('display','none');
		}else{
			//最大化
			$('#dianliao .body').stop().animate({'height':animate.bodyHeight},200);
			$('#dianliao').stop().animate({'bottom':parseInt($('#dianliao').css('bottom'))+parseInt(animate.bodyHeight)},200);
			$('#dianliao .win-min .max').removeClass('max').addClass('min');
			$('#dianliao .control .user-name').css('display','none');
			$('#dianliao .control .menu').css('display','block');
		}
	});
	/*聊天窗口最小化和關閉*/
	animate.tallHeight=$('#dl-tall-win').css('height');
	animate.tallWidth=$('#dl-tall-win').css('width');
	/*$('#dl-tall_win .tall-header .win-min .min').on('click',function(){
		$('#dl-tall_win').stop().animate({'height':'0px'},200);
	});*/
	$('#dl-tall_win .tall-header .win-min .exit').on('click',function(){
		$('#dl-tall_win').stop().animate({'width':'0px'},200);
		$('#dianliao .control .tall-win-open div').css('display','block');
	});
	/*列表切换*/
	$('#dianliao .menu .menu-body div').on('click',function(){
		$('#dianliao .menu .menu-body').find('.on').removeClass('on');
		$(this).addClass('on');
		$('.dl-win').removeClass('on');
		$('#'+$(this).attr('value')).addClass('on');
	});
	$('#dianliao .menu').on('click',function(){
		$('.dl-win').removeClass('on');
		$('#'+$('#dianliao .menu .menu-body').find('.on').attr('value')).addClass('on');
	});
	/*清空聊天列表*/
	$('#dl-tall-win .tall-menu-clear').on('click',function(){
		$('#tall-menu li').each(function(){
			if($(this).hasClass('on')){
			}else{
				$(this).find('i').click();
			}
		});
	});
	/*聊天窗口顯示*/
	$('#dianliao .control .tall-win-open').on('click',function(){
		$(this).find('div').css('display','none');
		$('#dl-tall_win').animate({'height':animate.tallHeight,'width':animate.tallWidth},200);//聊天窗口显示
	});
	/*菜单动画*/
	$('#dianliao .body .dl-menu .dl-menu-content .menu-c').hover(function(){
		if($(this).hasClass('on')){return;}
		$(this).stop().animate({'borderRadius':'22px'},100);
	},function(){
		if($(this).hasClass('on')){return;}
		$(this).stop().animate({'borderRadius':'0px'},100);
	});
	$('#dianliao .body .dl-menu .dl-menu-content .menu-c').on('click',function(){
		var t=$(this).attr('value');
		$('.dl-menu-content').attr('value',t);
		switch(t){
			case 'm1':
				$('.menu-content').animate({'opacity':'1'},200);
				$('#dianliao .body .dl-menu .dl-menu-content .tip-title').html($(this).html());
				$('#dianliao .body .dl-menu .dl-menu-content .dl-m-text').val('').focus();
			break;
			case 'm2':
				$('.menu-content').animate({'opacity':'1'},200);
				$('#dianliao .body .dl-menu .dl-menu-content .tip-title').html($(this).html());
				$('#dianliao .body .dl-menu .dl-menu-content .dl-m-text').val('').focus();
			break;
			case 'm3':
				$('.menu-content').animate({'opacity':'1'},200);
				$('#dianliao .body .dl-menu .dl-menu-content .tip-title').html($(this).html());
				$('#dianliao .body .dl-menu .dl-menu-content .dl-m-text').val(dotnerDianliao.dl.user.getUser().nick).focus();
			break;
			case 'm4':
				$('.menu-content').animate({'opacity':'0'},200);
				$('#dl-tall_win .tall-header .win-min .exit').click();
				//最小化
				$('#dianliao .body').stop().animate({'height':'0px'},200);
				$('#dianliao').stop().animate({'bottom':parseInt($('#dianliao').css('bottom'))-parseInt(animate.bodyHeight)},200);
				$('#dianliao .win-min .min').removeClass('min').addClass('max');
				$('#dianliao .control .user-name').css('display','block');
				$('#dianliao .control .menu').css('display','none');
				$('#dianliao .control').prepend('<div id="dl-m4-close"><div></div></div>');
				$('#dianliao').animate({'right':'40px'},200);
				$('#dianliao .control').animate({'width':'25px'},200);
				$('#dl-m4-close').on('click',function(){
					//最大化
					$('#dianliao .control #dl-m4-close').remove();
					$('#dianliao').animate({'right':animate.dlRight},200);
					$('#dianliao .control').animate({'width':animate.controlWidth},200);
					$('#dianliao .body').animate({'height':animate.bodyHeight},200);
					$('#dianliao').animate({'bottom':parseInt($('#dianliao').css('bottom'))+parseInt(animate.bodyHeight)},200);
					$('#dianliao .win-min .max').removeClass('max').addClass('min');
					$('#dianliao .control .user-name').css('display','none');
					$('#dianliao .control .menu').css('display','block');
				});
			break;
			case 'm5'://
			break;
			case 'm6'://
			break;
			case 'm7'://
			break;
			case 'm8'://
			break;
		}
	});
	$('.menu-content .dl-m-submit').on('click',function(){
		var t=$('.dl-menu-content').attr('value');
		var content=$('.menu-content .dl-m-text').val();
		switch(t){
			case 'm1':
				dotnerDianliao.dl.post.addFriend(content,0);
			break;
			case 'm2':
				var number=dotnerDianliao.dl.user.getFriendNumber(content);
				dotnerDianliao.dl.post.delFriend(number,0);
			break;
			case 'm3':
			break;

		}
	});
	/*菜单*/
	$('#dl-menu .m-font').on('click',function(){
		if($(this).parent().find('.dl-menu-content').css('display')=='block'){
			$(this).parent().find('.dl-menu-content').css('display','none');
			return false;
		}
		$(this).parent().find('.dl-menu-content').css({'display':'block','opacity':'0'});
		$(this).parent().find('.dl-menu-content').stop().animate({'opacity':'1'},300);
	});
}
/*关闭最近聊天列表的好友显示*/
function recentlyFriendClose(a){
	var b=$(a).parent().parent();
	var number=b.attr('number');
	dotnerDianliao.dl.post.delFriend(number,1);
}
function displayTallWin(number,nick,img,num){
	//消去此人未读消息数目
	if($('#dl-tip').html()!=''){
		$('#dl-tip').html($('#dl-tip').html()-(num));
		if ($('#dl-tip').html()==0){
			$('#dl-tip').html('');
		}
	}
	dotnerDianliao.dl.post.gossipOk(number);
	$('.f-'+number).find('.tip').remove();
	$('#dl-tall-win .tall-friend-msg .friend-avatar').html(img);
	$('#dl-tall-win .tall-friend-msg .friend-name').html(nick);
	//加入聊天列表
	$('#tall-menu').find('.on').removeClass('on');
	if($('#tall_'+number).length>0){
		$('#tall_'+number).addClass('on');
	}else{
		var maxNum=10;
		if($('#tall-menu li').length>=maxNum){
			if($('#tall-menu li').eq(maxNum-1).hasClass('on')){
				$('#tall-menu li').eq(maxNum-2).find('i').click();
			}else{
				$('#tall-menu li').eq(maxNum-1).find('i').click();
			}
		}
		$('#tall-menu').prepend('<li title="'+nick+'" class="on f-'+number+'" id="tall_'+number+'" number="'+number+'" onclick="tallMenuFriend(this,event);" ><a>'+nick+'</a><i onclick="tallMenuFriendClose(this);" clickOnly="1">×</i></li>');
	}
	if($('#dl-tall_win').css('width')=='0px'){
		$('#dl-tall_win').animate({'height':animate.tallHeight,'width':animate.tallWidth},200);//聊天窗口显示
	}
	try{
		$('#dl-tall-win .dl-msg .msg').css('display','none');
		if($('.msg-'+number).css('display')=='none'){
			$('.msg-'+number).css('display','block');
		}else{
			$('#dl-tall-win .dl-msg').append('<div class="msg msg-'+number+'"></div>');
		}
	}catch(err){
		$('#dl-tall-win .dl-msg').append('<div class="msg msg-'+number+'"></div>');
	}
}